package com.walker.taogong.dal.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.dal.TaskDal;
import com.walker.taogong.datamodel.Task;
import com.walker.taogong.datamodel.User;
import com.walker.taogong.hibernate.HibernateUtil;

public class TaskDalImpl extends BaseDal implements TaskDal
{

    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(TaskDalImpl.class);
    
    @Override
    public void saveTask(Task task)
    {
        HibernateUtil.getCurrentSession().saveOrUpdate(task);
        HibernateUtil.commit();
        
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Task> getTasksByUser(User user)
    {
        String hql = " from Task t where t.owner = :userName";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql)
                .setParameter("userName", user.getUserName());
        
        return query.list();
    }
    
    @Override
    public List<Task> getAllTasks()
    {
        String hql = " from Task";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql);
        
        return query.list();
    }
    
    @Override
    public Task getTaskById(int id)
    {
        Task name =(Task) HibernateUtil.getCurrentSession().get(Task.class,id);
        HibernateUtil.commit();

        return name;
    }
    
    /*************************启动組件**********************************************/
    /**
     * 组件启动初期化
     */
    @Override
    protected void activate(ComponentContext cc) 
    {
        log.info("{} is starting!", cc.getProperties().get("name") );
        super.activate(cc);
        
    }





}
